package com.oracle.lingshangejia.utils;

import android.util.Log;

/**
 * 日志打印工具类
 */
public class MyLogger {
    /**
     * 日志显示的标签
     */
    private static String TAG = "MyLogger";


    private static boolean IS_FULL_CLASSNAME;

    /**
     * 日志显示的最低等级
     */
    private static int LOG_LEVEL = Log.VERBOSE;

    /**
     * print full class name or not
     *
     * @param isFullClassName
     */
    public static void setFullClassName(boolean isFullClassName) {
        MyLogger.IS_FULL_CLASSNAME = isFullClassName;
    }

    /**
     * 定义显示日志的最低等级,软件编写完成后,可以将这个值改为8,整个程序都不会打印日志
     *
     * @param level
     */
    public static void setLogLevel(int level) {
        MyLogger.LOG_LEVEL = level;
    }

    /**
     * 定义日志显示的标签
     *
     * @param tag
     */
    public static void setAppTAG(String tag) {
        MyLogger.TAG = tag;
    }


    public static void v(String msg) {
        if (LOG_LEVEL <= Log.VERBOSE) {
            Log.v(TAG, getLogTitle() + msg);
        }
    }


    public static void d(String msg) {
        if (LOG_LEVEL <= Log.DEBUG) {
            Log.d(TAG, getLogTitle() + msg);
        }
    }

    public static void i(String msg) {
        if (LOG_LEVEL <= Log.INFO) {
            Log.i(TAG, getLogTitle() + msg);
        }
    }

    public static void w(String msg) {
        if (LOG_LEVEL <= Log.WARN) {
            Log.w(TAG, getLogTitle() + msg);
        }
    }

    public static void e(String msg) {
        if (LOG_LEVEL <= Log.ERROR) {
            Log.e(TAG, getLogTitle() + msg);
        }
    }

    /**
     * 自动生成log的标题,标题内容为方法名加当前的行号
     *
     * @return
     */
    private static String getLogTitle() {
        StackTraceElement elm = Thread.currentThread().getStackTrace()[4];
        String className = elm.getClassName();
        if (!IS_FULL_CLASSNAME) {
            int dot = className.lastIndexOf('.');
            if (dot != -1) {
                className = className.substring(dot + 1);
            }
        }
        return className + "." + elm.getMethodName() + "(" + elm.getLineNumber() + ")" + ": ";
    }

    /**
     * 分段打印出较长log文本
     * @param logContent  打印文本
     */
    public static void showLargeLog(String logContent){
        int showLength =3900;
        if(logContent.length() > showLength){
            String show = logContent.substring(0, showLength);
            Log.i(TAG,getLogTitle() + show);
            /*剩余的字符串如果大于规定显示的长度，截取剩余字符串进行递归，否则打印结果*/
            if((logContent.length() - showLength) > showLength){
                String partLog = logContent.substring(showLength,logContent.length());
                showLargeLog(partLog);
            }else{
                String printLog = logContent.substring(showLength, logContent.length());
                Log.i(TAG,getLogTitle()+ printLog);
            }
        }else{
            Log.i(TAG ,getLogTitle() + logContent);
        }
    }


}